package com.liliudong.cursormate.common;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * R
 *
 * @author liliudong
 * @version 1.0
 * @description 全局统一响应类
 * @date 2023/7/26 15:56
 */
@AllArgsConstructor
@Getter
public class R<T> {
    private final static String SUCCESS = "success";

    private int code;
    private String msg;
    private T data;


    /**
     * 成功
     *
     * @param data 数据
     * @param msg  msg
     * @param code code
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(T data, String msg, int code) {
        return new R<>(code, msg, data);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @param msg  msg
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(T data, String msg) {
        return success(data, msg, 1);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(T data) {
        return success(data, SUCCESS);
    }

    /**
     * 成功
     *
     * @return {@link R}<{@link ?}>
     */
    public static R<?> success() {
        return success(null);
    }

    public static R<?> error(int code, String msg) {
        return new R<>(code, msg, null);
    }

    /**
     * 错误
     *
     * @param code 代码
     * @return {@link R}<{@link ?}>
     */
    public static R<?> error(int code) {
        return error(code, "error");
    }

    /**
     * 错误
     *
     * @param msg msg
     * @return {@link R}<{@link ?}>
     */
    public static R<?> error(String msg) {
        return error(0, msg);
    }

    /**
     * 错误
     *
     * @return {@link R}<{@link ?}>
     */
    public static R<?> error() {
        return error(0);
    }
}
